// ---- 引入Router库相应函数。
import { createRouter, createWebHashHistory } from "vue-router";
// ---- 引入相关页面组件。
import HomePage from '@/views/HomePage.vue';
import LoginPage from '@/views/LoginPage.vue';
import CustomerListPage from '@/views/customer/CustomerListPage.vue';
import CustomerDetailPage from '@/views/customer/CustomerDetailPage.vue';
import StaffListPage from '@/views/staff/StaffListPage.vue';
import StaffAuthorityPage from '@/views/staff/StaffAuthorityPage.vue';
import AppListPage from '@/views/app/AppListPage.vue';
import AppUpsertPage from '@/views/app/AppUpsertPage.vue';
import AuthorityTreePage from '@/views/authority/AuthorityTreePage.vue';
import RoleListPage from '@/views/role/RoleListPage.vue';
import RoleUpsertPage from '@/views/role/RoleUpsertPage.vue';

const routes = [
    {
        path: '/',
        name: 'Login',
        component: LoginPage,
        meta: { requiresAuth: false }
    },
    {
        path: '/home',
        name: 'Home',
        component: HomePage,
        children: [
            {
                path: 'customer_list',
                name: 'CustomerList',
                component: CustomerListPage
            },
            {
                path: 'customer_detail/:customerId',
                name: 'CustomerDetail',
                component: CustomerDetailPage
            },
            {
                path: 'staff_list',
                name: 'StaffList',
                component: StaffListPage
            },
            {
                path: 'staff_authority/:staffAccount',
                name: 'StaffAuthority',
                component: StaffAuthorityPage
            },
            {
                path: 'app_list',
                name: 'AppList',
                component: AppListPage
            },
            {
                path: 'app_upsert/:appCode?',
                name: 'AppUpsert',
                component: AppUpsertPage
            },
            {
                path: 'authority_tree',
                name: 'AuthorityTree',
                component: AuthorityTreePage
            },
            {
                path: 'role_list',
                name: 'RoleList',
                component: RoleListPage
            },
            {
                path: 'role_upsert/:roleCode?',
                name: 'RoleUpsert',
                component: RoleUpsertPage
            }
        ]
    }
]

// ---- 创建路由器。
const router = createRouter({
    // ---- 设置历史记录模式为哈希模式，即使用URL的哈希部分（URL中#符号及其后续部分）来表示路
    // 由路径。这是Router的默认模式。
    history: createWebHashHistory(),
    // ---- 设置路由集为上游定义的路由集。如果自定义的路由集与参数同名，那么可以直接简写为routes。
    routes: routes
})

// ---- 路由守卫：用于在正式跳转前执行自定义逻辑，例如检查当前用户是否登录。
router.beforeEach((to, from, next) => {
    // ---- 检查页面是否需要校验登录/用户是否登录，否则跳转至登录页。
    const token = !!localStorage.getItem('token');
    const isAuth = to.matched.some(record => record.meta && record.meta.requiresAuth === false);
    if (token || isAuth) {
        // ---- 如果已登录或页面无需校验，跳转至指定页面。
        next();
    } else {
        // ---- 页面需校验但未登录，跳转至登录页。
        next({ name: 'Login' });
    }
});

export default router
